package com.cheyunkeji.er.activity.auction;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.auction.CooperatorBaseInfo;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.utils.ChannelInfoUtil;
import com.cheyunkeji.er.view.SDialog;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

public class SubmitInfoActivity extends BaseActivity implements SDialog.SDialog2Listener {


    private static final String TAG = SubmitInfoActivity.class.getSimpleName() ;
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tv_csmc)
    TextView tvCsmc;
    @BindView(R.id.tv_administrator)
    TextView tvAdministrator;
    @BindView(R.id.tv_contact_number)
    TextView tvContactNumber;
    @BindView(R.id.tv_apply_cooperate)
    TextView tvApplyCooperate;
    @BindView(R.id.activity_setting)
    LinearLayout activitySetting;
    private CooperatorBaseInfo cooperatorBaseInfo;

    private static final String CAR_DEALER_INFO_NOT_COMPLETION = "3" ;

    private SDialog sDialog ;

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_submit_info);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {

        vTopbar.setLeftBack();
        vTopbar.setTitle("申请合作");
        tvApplyCooperate.setOnClickListener(this);
        sDialog = new SDialog(this,R.string.t_message_title, true , getResources().getString(R.string.go_to_complete_dealer_info), this, SDialog.TYPE_2_BTN);
    }

    @Override
    protected void initData() {

        if (null != getIntent().getSerializableExtra("COOPERATOR_INFO")) {

            cooperatorBaseInfo = (CooperatorBaseInfo) getIntent().getSerializableExtra("COOPERATOR_INFO");
            displayInfo(cooperatorBaseInfo);
        }
    }


    private void displayInfo(CooperatorBaseInfo cooperatorBaseInfo) {

        tvCsmc.setText(cooperatorBaseInfo.getTitle());
        tvAdministrator.setText(String.format("系统管理员： %1$s",cooperatorBaseInfo.getAdmin_name()));
        if(!TextUtils.isEmpty(cooperatorBaseInfo.getMobile())){
            tvContactNumber.setText(cooperatorBaseInfo.getMobile().replaceFirst(cooperatorBaseInfo.getMobile().substring(3, 7), "****"));
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_apply_cooperate:

                if (null != cooperatorBaseInfo) {

                    goToBindChannel();
                }

                break;

            default:
                break;
        }
    }


    private void goToBindChannel() {

        HashMap<String, String> params = new HashMap<>();
//        params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//        params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
        params.put("cid", cooperatorBaseInfo.getId());
        params.put("bcid", ChannelInfoUtil.getCurrentChannelInfo().getChannel());

        ApiClient.postForm(HttpConstants.BIND_CHANNEL, params, new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {

                // 网络异常
                if (e instanceof UnknownHostException || e instanceof SocketException) {
                    SToast.show(R.string.tip_network_error, Gravity.CENTER);
                    return;
                }

            }

            @Override
            public void onResponse(String response, int id) {

                Log.e(TAG, "onResponse: " + response );
                try {

                    JSONObject obj = new JSONObject(response);

                    //  车商信息不完整时，返回code值为 3
                    if(TextUtils.equals(obj.optString("code"), CAR_DEALER_INFO_NOT_COMPLETION)){
                        sDialog.show();
                        return ;
                    }

                    if (obj.optInt("code") == 1) {
                        SToast.show("申请成功");
                        Intent intent = new Intent(SubmitInfoActivity.this,ApplyRecordActivity.class);
                        SubmitInfoActivity.this.startActivity(intent);
                    } else {
                        SToast.show(obj.optString("msg"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onBefore(Request request, int id) {
                showDialog();
            }

            @Override
            public void onAfter(int id) {
                dismissDialog();
            }

        });
    }

    @Override
    public void onClick(boolean l, View view) {

        if(!l){
            Intent intent = new Intent(this,EditPersonalInfoActivity.class);
            intent.putExtra("IS_FROM_BINDER",true );
            startActivity(intent);
        }
    }

}
